namespace Billing2
{
    using AjaxControlToolkit;
    using Billing2.Controls;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MyLib;

    public class GenerateBillData : Page
    {
        protected Button BtnGenerate;
        protected ConfirmButtonExtender BtnGenerate_ConfirmButtonExtender;
        protected CheckBox chkboxOverwriteData;
        protected DropDownList ddlBillPeriod;
        protected ucHighlight ucHighlight1;
        protected ucPageTitle ucPageTitle1;
        protected UpdatePanel updatePanel1;
        protected UpdatePanel updatePanel2;
        protected UpdateProgress updateprogress1;

        private void BindBillPeriod()
        {
            ClsBillPeriod cls1 = new ClsBillPeriod();
            this.ddlBillPeriod.DataSource = cls1.SelectAll();
            this.ddlBillPeriod.DataTextField = "ServiceMonth";
            this.ddlBillPeriod.DataValueField = "ID";
            this.ddlBillPeriod.DataBind();
            DataTable dtCurrentPeriod = cls1.SelectCurrentPreiod();
            if ((dtCurrentPeriod.Rows != null) && (dtCurrentPeriod.Rows.Count > 0))
            {
                this.BillPeriodID = dtCurrentPeriod.Rows[0]["ID"].ToString();
            }
        }

        protected void BtnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.chkboxOverwriteData.Checked && ClsBillCommon.IsMonthlyExists(Convert.ToInt64(this.BillPeriodID)))
                {
                    this.ucHighlight1.ErrorMessage = "Existing billing data found on the selected billing period. Unable to proceed.";
                }
                else
                {
                    string strOutput = ClsBillCommon.GenerateBillData(Convert.ToInt64(this.BillPeriodID));
                    strOutput = "Billing data has been generated for " + this.ddlBillPeriod.Items[this.ddlBillPeriod.SelectedIndex].Text + ".<br />" + strOutput;
                    this.ucHighlight1.Message = strOutput;
                }
            }
            catch (Exception ex)
            {
                EPPLog.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
            }
        }

        private void GetPermission()
        {
            ClsACLHelper cls = new ClsACLHelper();
            string pageName = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            cls.SetPermission(LoginUser.TreeMenu, pageName);
            this.BtnGenerate.Visible = cls.Create0;
            if (!cls.Read0)
            {
                base.Response.Redirect("logout.aspx",true);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ucHighlight1.CloseAll();
            if (this.Session["SiteID"] == null)
            {
                base.Response.Redirect("default.aspx",true);
            }
            if (!this.Page.IsPostBack)
            {
                this.BindBillPeriod();
            }
            this.GetPermission();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ViewStateUserKey = Session.SessionID;
        }

        public string BillPeriodID
        {
            get
            {
                if (this.ddlBillPeriod.SelectedIndex == -1)
                {
                    return "0";
                }
                return this.ddlBillPeriod.SelectedValue;
            }
            set
            {
                this.ddlBillPeriod.SelectedIndex = -1;
                if ((value != "") && (value != "0"))
                {
                    this.ddlBillPeriod.SelectedIndex = this.ddlBillPeriod.Items.IndexOf(this.ddlBillPeriod.Items.FindByValue(value));
                }
            }
        }
    }
}
